
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="国家" prop="country">
        <el-input
          v-model="queryParams.country"
          placeholder="请输入允许的国家"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备" prop="device">
        <el-input
          v-model="queryParams.device"
          placeholder="请输入允许的设备"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="域名" prop="domain">
        <el-input
          v-model="queryParams.domain"
          placeholder="请输入绑定域名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采集网址" prop="mirror">
        <el-input
          v-model="queryParams.mirror"
          placeholder="请输入采集网址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="LP网址" prop="landing_page">
        <el-input
          v-model="queryParams.landing_page"
          placeholder="请输入投放产品网址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跳转形式" prop="jump">
        <el-input
          v-model="queryParams.jump"
          placeholder="请输入跳转形式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleCopy"
        >复制</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        选择后两种跳转形式请一定点击<strong>跳转测试</strong>按钮进行测试，看看到底加载的是否完整，能否正常下单。
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="domainList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
             <el-table-column label="id" align="center" prop="id" width="40" />
             <el-table-column label="允许国家" align="center" prop="country" />
             <el-table-column label="允许设备" align="center" prop="device" width="80"/>
             <el-table-column label="绑定域名" align="center" prop="domain" />
             <el-table-column label="采集网址" align="center" prop="mirror" />
             <el-table-column label="LP网址" align="center" prop="landing_page" />
             <el-table-column label="跳转形式" align="center" prop="jump" width="80" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-test"
                @click="handleNewPageLink(scope.row)"
              >跳转测试</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleCopy(scope.row)"
              >复制</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
            <!-- 添加或修改用户与其绑定的域名对话框 -->
                <el-dialog :close-on-click-modal=false :title="title" :visible.sync="open" width="500px" append-to-body>
                  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                    <el-form-item label="投放国家" prop="country">
                      <el-input v-model="form.country" placeholder="请输入允许的国家缩写，多个请用逗号分割，例如US,CN" />
                    </el-form-item>
                    <el-form-item label="允许设备" prop="device">
<!--                      <el-input v-model="form.device" placeholder="请输入允许的设备" />-->
                      <el-select v-model="form.device" placeholder="请选择允许的设备">
                        <el-option
                          v-for="item in devices"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="绑定域名" prop="domain">
<!--                      <el-input v-model="form.domain" placeholder="请输入绑定域名，例如www.test.com" />-->
                      <el-select v-model="form.domain" placeholder="请选择您已经绑定的域名">
                        <el-option
                          v-for="item in bindDomainList"
                          :key="item.bind_domain"
                          :label="item.bind_domain"
                          :value="item.bind_domain">
                        </el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="采集网址" prop="mirror">
                      <el-input v-model="form.mirror" placeholder="采集目标网址，例如https://www.sanfenzui.com" />
                    </el-form-item>
                    <el-form-item label="LP网址" prop="landing_page">
                      <el-input v-model="form.landing_page" placeholder="着陆页或产品网址，例如https://www.myoffer.com" />
                    </el-form-item>
                    <el-form-item label="跳转形式" prop="jump">
<!--                      <el-input v-model="form.jump" placeholder="请输入跳转形式" />-->
                      <el-select v-model="form.jump" placeholder="请选择跳转形式">
                        <el-option
                          v-for="item in jumps"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                      跳转形式对比详见后台首页
                    </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </div>
</template>
<script>
import { listDomain, getDomain, delDomain, addDomain, updateDomain } from '@/api/module/domain'
import {allNoUsedBindDomain} from '@/api/cloakConfig/bindDomain'
export default {
  name: "domain",
  data() {
    return {
          // 遮罩层
          loading: true,
          // 选中数组
          ids: [],
          // 非单个禁用
          single: true,
          // 非多个禁用
          multiple: true,
          // 显示搜索条件
          showSearch: true,
          // 总条数
          total: 0,
          // 用户与其绑定的域名表格数据
          domainList: [],
          // 绑定域名表格数据
          bindDomainList: [],
          // 弹出层标题
          title: "",
          // 是否显示弹出层
          open: false,
          // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          user_id: null,
          country: null,
          device: null,
          domain: null,
          mirror: null,
          landing_page: null,
          jump: null,
        },
      devices: [{
          value: 'mobile',
          label: 'mobile'
        }, {
          value: 'pc',
          label: 'pc'
        }, {
          value: 'all',
          label: 'all'
        }],
      jumps: [{
          value: '直接跳转',
          label: '301直接跳转'
        }, {
          value: 'iframe加载',
          label: 'iframe加载'
        }, {
          value: '加载页面',
          label: '加载页面'
        }],
        // 表单参数
          form: {},
          // 表单校验
          rules: {
          }
      };
    },
    created() {
        this.getList();
        this.getBindDomainList();
    },
    methods: {
        /** 查询用户与其投放列表 */
        getList() {
          this.loading = true;
          listDomain(this.queryParams).then(response => {
            this.domainList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          });
        },
      /** 查询用户与其绑定的域名列表 */
        getBindDomainList() {
          this.loading = true;
          allNoUsedBindDomain().then(response => {
            this.bindDomainList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          });
        },
        // 取消按钮
        cancel() {
          this.open = false;
          this.reset();
        },
        // 表单重置
        reset() {
          this.form = {
            id: null,
            user_id: null,
            country: null,
            device: null,
            domain: null,
            binddomain: null,
            mirror: null,
            landing_page: null,
            jump: null
          };
          this.resetForm("form");
        },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户与其绑定的域名";
    },
      /** 修改按钮操作 */
    handleNewPageLink(row) {
      this.reset();
      const domain = row.domain

      window.open("https://"+domain+"?id=test", '_blank').location;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDomain(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户与其绑定的域名";
      });
    },
        /** 复制按钮操作 */
    handleCopy(row) {
      this.reset();
      const id = row.id || this.ids
      getDomain(id).then(response => {
        this.form = response.data;
        this.open = false;
        // this.title = "修改绑定域名";
        addDomain(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess("复制成功");
                this.open = false;
                this.getList();
              }
            });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDomain(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addDomain(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户与其绑定的域名编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDomain(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    } //methods结束
};
</script>
